<?php
namespace Home\Model;
use Think\Model;
class GoodcategoryModel extends Model{
	public function getPidList($cid){
		//var_dump($cid);
		$pact = array();
			$cat = $this->field('GoodcategoryId','CategoryName','level')->where("GoodcategoryId=$cid")->find();  
		    $pact[] = array(   //把分列保存在数组
		           'cid'=>$cat['GoodcategoryId'],
		           'cname'=>$cat['CategoryName'],
		           'clevel'=>$cat['level'],
		    );
		return $pact;
	}

/**
	 * 获得分类列表(将所有的数据以树的形式展示)
	 */
	public function getList(){
		$data = $this->select();
		$data==null && $data = array();
		$data = $this->tree($data);
		return $data;
	}
	
	
	
/**
	 * 递归计算分类深度（$result包含并将该类的子孙及自己的数据【id、name、level(父id/上一个节点的id)】，默认采取整颗树）
	 */
	public function tree(&$data,$level = 0,$deep=0) {
		//保存按顺序查找到的分类
		static $result = array();
		//按照level查找
		foreach ($data as $row) {
			if ($row['level'] == $level) {
				$row['deep'] = $deep;
				$result[] = $row;
				$this->tree($data, $row['GoodcategoryId'], $deep + 1);
			}
		}
		return $result;
	}
}
?>